package com.reins.bookstore.controller;
import com.reins.bookstore.constant.Constant;
import com.reins.bookstore.entity.Basketline;
import com.reins.bookstore.entity.Book;
import com.reins.bookstore.object.*;
import com.reins.bookstore.service.BasketService;
import com.reins.bookstore.service.BookService;
import com.reins.bookstore.service.OrderService;
import com.reins.bookstore.service.UserService;
import com.reins.bookstore.utils.filesutils.FileUtils;
import com.reins.bookstore.utils.msgutils.Msg;
import com.reins.bookstore.utils.msgutils.MsgCode;
import com.reins.bookstore.utils.msgutils.MsgUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
public class HelloController {



    @Autowired
    private BookService bookService;


    @Autowired
    private BasketService basketService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @RequestMapping("/test/writeDocuments")
    public Msg WriteDocument(){
        List<BookDTO> res = bookService.getBooks();
        JSONArray array = new JSONArray(res);
        FileUtils.writeJsonFile(array.toString(),FileUtils.BOOK_DATA);

        List<Basketline> basketlines = basketService.findAllBuylists();
        array = new JSONArray(basketlines);
        FileUtils.writeJsonFile(array.toString(),FileUtils.BASKET_LINE_DATA);

        List<UserDTO> userDTOList = userService.findAllUsers();
        array = new JSONArray(userDTOList);
        FileUtils.writeJsonFile(array.toString(),FileUtils.USER_DATA);


        List<OrderDTO> orderDTOList =  orderService.findGlobalOrders();
        array = new JSONArray(orderDTOList);
        FileUtils.writeJsonFile(array.toString(),FileUtils.ORDERS_DATA);


        return MsgUtil.makeMsg(MsgCode.SUCCESS);
    }

    @RequestMapping("/hello")
    public Msg Greeting(@RequestBody Greet params){
        return MsgUtil.makeMsg(MsgCode.SAY_HELLO);

    }
    @RequestMapping("/test")
    public Msg GTest(@RequestBody Map<String, String> params){
        return MsgUtil.makeMsg(MsgCode.SAY_HELLO);
    }
}
